﻿using ProjectScheduling.daos;
using ProjectScheduling.models;

namespace ProjectScheduling
{
    public partial class AddSalesForm : Form
    {
        public readonly static AddSalesForm b = new();
        private Sales? _sales { get; set; }
        public Action? action_ok;
        public AddSalesForm()
        {
            _sales = new();
            InitializeComponent();
            ChangeFunc();
        }
        public void Init(int id)
        {
            _sales = id == 0 ? new() : SaleDao.Base.Find(id);
            txt_name.Text = _sales.Name;
            txt_phone.Text = _sales.Phone;
            txt_truename.Text = _sales.TrueName;
            r_0.Checked = _sales.Sex == "女";
            r_1.Checked = _sales.Sex == "男";
            ch_lock.Checked = _sales.IsLock;
            ch_isAdmin.Checked = _sales.IsAdmin;
            txt_pass.Text = "";
        }
        /// <summary>
        /// 控件Change事件
        /// </summary>
        private void ChangeFunc()
        {
            if (_sales == null) return;

            txt_name.TextChanged += (o, e) => _sales.Name = txt_name?.Text;
            txt_pass.TextChanged += (o, e) => _sales.Password = txt_pass.Text;
            txt_phone.TextChanged += (o, e) => _sales.Phone = txt_phone.Text;
            txt_truename.TextChanged += (o, e) => _sales.TrueName = txt_truename.Text;
            r_0.CheckedChanged += (o, e) => { if (r_0.Checked) { _sales.Sex = "女"; } };
            r_1.CheckedChanged += (o, e) => { if (r_1.Checked) { _sales.Sex = "男"; } };
            ch_lock.CheckedChanged += (o, e) => _sales.IsLock = ch_lock.Checked;
            ch_isAdmin.CheckedChanged += (o, e) => _sales.IsAdmin = ch_isAdmin.Checked;
        }
        private void ShowError(string msg)
        {
            MessageBox.Show(msg, "错误提醒");
        }
        private void Btn_save_Click(Object sender, EventArgs e)
        {
            if (_sales == null) return;
            if (string.IsNullOrEmpty(_sales.Name))
            {
                ShowError("登录名不能为空");
                return;
            }
            if (_sales.Id == 0)
            {
                if (string.IsNullOrEmpty(_sales.Password))
                {
                    ShowError("密码不能为空");
                    return;
                }
                _sales.Password = Common.Md5(_sales.Password);
                _sales.CreateTime = DateTime.Now;
                SaleDao.Base.Add(_sales);
            }
            else
            {
                if (string.IsNullOrEmpty(_sales.Password))
                {
                    SaleDao.Base.Update(_sales, it => new { it.Name, it.TrueName, it.Sex, it.IsLock, it.Phone,it.IsAdmin });
                }
                else
                {
                    _sales.Password = Common.Md5(_sales.Password);
                    SaleDao.Base.Update(_sales, it => new { it.Name, it.TrueName, it.Sex, it.IsLock, it.Phone, it.Password, it.IsAdmin });
                }
            }
            MessageBox.Show("添加成功");
            action_ok?.Invoke();
            Close();
        }
    }
}
